package com.hu.zhcc.shiro.controller.lgx;

import com.alibaba.fastjson.JSONObject;
import com.hu.zhcc.shiro.entity.lgx.*;
import com.hu.zhcc.shiro.entity.xsf.pojo.TDisease;
import com.hu.zhcc.shiro.service.lgx.*;
import com.hu.zhcc.shiro.service.xsf.TDiseaseLibraryService;
import com.hu.zhcc.shiro.service.xsf.TDiseaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class TmemberController {
    @Autowired
    //会员表
    TmemberService tmemberService;
    @Autowired
    //健康师表
    ThealthmgerService thealthmgerService;
    @Autowired
    //会员动态表
    TMemberDynamicinfoService tMemberDynamicinfoService;
    @Autowired
    //会员病历表
    TMembermedicalhistoryService tMembermedicalhistoryService;
    @Autowired
    //会员详情
    TmemberInfoService tmemberInfoService;
    //预约信息
    @Autowired
    TOrderService tOrderService;
    @Autowired
    //套餐显示
    TSetmealService tSetmealService;
    //会员检查表
    @Autowired
    TMemberExaminationService tMemberExaminationService;
    //会员注册次数表
    @Autowired
    TMemberRegisterService tMemberRegisterService;
    //疾病库
    @Autowired
    TDiseaseLibraryService tDiseaseLibraryService;
    //疾病库中间表
    @Autowired
    TDiseaseService tDiseaseService;
    //会员显示
    @GetMapping("/tMemberLis")
    public  List<TMember> tMemberList(){
        List<TMember> tMembers = tmemberService.TmemberList();
        return tMembers;
    }
    //健康师显示
    @GetMapping("/tHealthmgrList")
    public  List<THealthmgr> tHealthmgrList(){
        List<THealthmgr> tHealthmgrs = thealthmgerService.thealthmgrList();
        return tHealthmgrs;
    }
    //会员动态查询id
    @PostMapping("/tMemberDynamicinfo")
    public TMemberDynamicinfo tMemberDynamicinfo(@RequestBody Map<String, String> map){
        TMemberDynamicinfo tMemberDynamicinfo = tMemberDynamicinfoService.selectByPrid(Integer.parseInt(map.get("id")));
        return tMemberDynamicinfo;
    }
    //会员病历查询id
    @PostMapping("/tMembermedicalhistory")
    public TMemberMedicalhistory tMembermedicalhistory(@RequestBody Map<String, String> map){
        TMemberMedicalhistory id = tMembermedicalhistoryService.TMemid(Integer.parseInt(map.get("id")));
        return id;
    }
    //会员详情修改
    @PostMapping("/tMemberInfoup")
    public int tMemberInfoup(@RequestBody TMemberInfo tMemberInfo){
        int i = tmemberInfoService.updateByPrimaryKey(tMemberInfo);
        return i;
    }
    //会议档案添加
    @PostMapping("/archiveadd")
    public int archiveadd(@RequestBody Map<String,String> map){
        String archivesStr = map.get("archives");
        Archives archives = JSONObject.parseObject(archivesStr, Archives.class);
        Date date=new Date();
        SimpleDateFormat formats=new SimpleDateFormat("YYYY-MM-dd");
        String format1 = formats.format(date);
        archives.getTMember().setRegtime(date);
        archives.getTMember().setCreatetime(date);
        int insert = tmemberService.insert(archives.getTMember());
        Integer id =archives.getTMember().getId();


//        病历史
        String medicalHistory="";
//        家族史
        String familyHistory="";

        String dearDiseaseHas = map.get("dearDiseaseHas");
        Integer[] dearDisease = JSONObject.parseObject(dearDiseaseHas, Integer[].class);
        for(int i=0;i<dearDisease.length;i++){
            TDisease tDisease = new TDisease();
            tDisease.setDiseaseLibraryLib(dearDisease[i]);
            //父母
            tDisease.setWho(1);
            tDisease.setMemberId(id);
            tDiseaseService.insertSelective(tDisease);

            familyHistory+=tDiseaseLibraryService.getById(dearDisease[i]).getName();
            if(i<dearDisease.length-1){
                familyHistory+=",";
            }
        }

        String myDiseaseHas = map.get("myDiseaseHas");
        Integer[] myDisease = JSONObject.parseObject(myDiseaseHas, Integer[].class);
        for(int i=0;i<myDisease.length;i++){
            TDisease tDisease = new TDisease();
            tDisease.setDiseaseLibraryLib(myDisease[i]);
            //本人
            tDisease.setWho(0);
            tDisease.setMemberId(id);
            tDiseaseService.insertSelective(tDisease);

            medicalHistory+=tDiseaseLibraryService.getById(myDisease[i]).getName();
            if(i<myDisease.length-1){
                medicalHistory+=",";
            }
        }


        TMemberRegister tMemberRegister1 = tMemberRegisterService.tmemberRegDate(format1);
        System.out.println(tMemberRegister1+"会员注册");
        if(tMemberRegister1!=null){
            TMemberRegister tMemberRegister=new TMemberRegister();
                tMemberRegister.setId(tMemberRegister1.getId());
                tMemberRegister.setRegisterDate(tMemberRegister1.getRegisterDate());
                tMemberRegister.setTimes(tMemberRegister1.getTimes()+1);
        }else {
            TMemberRegister tMemberRegister=new TMemberRegister();
            tMemberRegister.setRegisterDate(format1);
            tMemberRegister.setTimes(1);
            tMemberRegisterService.insert(tMemberRegister);
        }
        archives.getFrom().setTMemberId(id);
        archives.getFrom().setCreatetime(date);
        archives.getFrom().setStatus(0);
        archives.getFromc().setTMemberId(id);
        archives.getFromc().setCreatetime(date);

        archives.getFromc().setFamilyHistory(familyHistory);
        archives.getFromc().setMedicalHistory(medicalHistory);

        archives.getFromx().setTMemberId(id);
        archives.getFromx().setCreatetime(date);
        archives.getFromyy().setMemberId(id);
        archives.getFromyy().setOrderdate(date);
        archives.getFromyy().setOrderstatus("未到诊");
        TMemberExamination tMemberExamination=new TMemberExamination();
        tMemberExamination.setTMemberId(id);
        tMemberExamination.setTSetmealId(archives.getFromyy().getSetmealId());
        tMemberExamination.setExamDate(archives.getFromyy().getOrderdate());
        tMemberExamination.setReportStatus(0);
        tMemberExamination.setCreatetime(date);
        int insert5 = tMemberExaminationService.insert(tMemberExamination);
        int insert1 = tmemberInfoService.insert(archives.getFrom());
        int insert2 = tMemberDynamicinfoService.insert(archives.getFromx());
        int insert3 = tMembermedicalhistoryService.insert(archives.getFromc());
        int insert4 = tOrderService.insert(archives.getFromyy());


        return insert;
    }
    //会员档案修改
    @PostMapping("/archiveup")
    public int archiveup(@RequestBody Archives archives){
        Date date=new Date();
        archives.getTMember().setUpdatetime(date);
        archives.getFrom().setUpdatetime(date);
        archives.getFromc().setUpdatetime(date);
        archives.getFromx().setUpdatetime(date);
        archives.getFromyy().setUpdatetime(date);
        TMemberExamination tMemberExamination = tMemberExaminationService.tmemberExid(archives.getTMember().getId());
        if(tMemberExamination==null){
            TMemberExamination tMemberExamination1=new TMemberExamination();
            tMemberExamination1.setTMemberId(archives.getTMember().getId());
            tMemberExamination1.setTSetmealId(archives.getFromyy().getSetmealId());
            tMemberExamination1.setReportStatus(0);
            tMemberExamination1.setExamDate(date);
            tMemberExamination1.setCreatetime(date);
            tMemberExamination1.setUpdatetime(date);
            int i5 = tMemberExaminationService.insert(tMemberExamination1);
        }else {
            tMemberExamination.setTSetmealId(archives.getFromyy().getSetmealId());
            tMemberExaminationService.updateByPrimaryKey(tMemberExamination);
        }
        TOrder tOrder = tOrderService.selectByPrimaryKey(archives.getFromyy().getId());
        if(tOrder==null){
            archives.getFromyy().setMemberId(archives.getTMember().getId());
            archives.getFromyy().setOrderdate(date);
            archives.getFromyy().setOrderstatus("未到诊");
            archives.getFromyy().setCreatetime(date);
            int insert = tOrderService.insert(archives.getFromyy());
            System.out.println(insert);
        }else if(!tOrder.getSetmealId().equals(archives.getFromyy().getSetmealId())){
            archives.getFromyy().setMemberId(archives.getTMember().getId());
            archives.getFromyy().setOrderdate(date);
            archives.getFromyy().setOrderstatus("未到诊");
            int insert = tOrderService.updateByPrimaryKey(archives.getFromyy());
        }
        int i = tmemberService.updateByPrimaryKey(archives.getTMember());
        int i1 = tmemberInfoService.updateByPrimaryKeySelective(archives.getFrom());
        if(archives.getFromx().getId()==null){
            archives.getFromx().setTMemberId(archives.getTMember().getId());
            int i2 = tMemberDynamicinfoService.insertSelective(archives.getFromx());
        }else {
            int i2 = tMemberDynamicinfoService.updateByPrimaryKeySelective(archives.getFromx());
        }
        if(archives.getFromc().getId()==null){
            archives.getFromc().setTMemberId(archives.getTMember().getId());
            int insert = tMembermedicalhistoryService.insert(archives.getFromc());
        }else {
            int i3 = tMembermedicalhistoryService.updateByPrimaryKeySelective(archives.getFromc());
        }
        int i4 = tOrderService.updateByPrimaryKey(archives.getFromyy());
        return i;
    }
    //预约信息
    @PostMapping("/tOrder")
    public TOrder tOrder(@RequestBody Map<String, String> map){
        TOrder id = tOrderService.selectBy(Integer.parseInt(map.get("id")));
        return id;
    }
    //套餐信息带条件
    @GetMapping("/tSetmealLists")
    public List<TSetmeal> tSetmealLists(){
        List<TSetmeal> tSetmeals = tSetmealService.tsetmealLists();
        return tSetmeals;
    }
    //套餐信息不带条件
    @GetMapping("/tSetmealList")
    public List<TSetmeal> tSetmealList(){
        List<TSetmeal> tSetmeals = tSetmealService.tsetmealList();
        return tSetmeals;
    }
    @PostMapping("/tsetmealid")
    public TSetmeal tsetmealid(@RequestBody Map<String, String> map){
        TSetmeal id = tSetmealService.selectByPrimaryKey(Integer.parseInt(map.get("id")));
        return id;
    }

    //通过姓名查询idcard
    @PostMapping("/queryIdCardByName/{name}")
    public List<TMember> queryIdCardByName(@PathVariable("name") String name){
        System.out.println(name);
        List<TMember> tMembers = tmemberService.selectByName(name);
        return tMembers;
    }
}
